package com.hx.vending.controller;

import com.hx.vending.annotation.AnnotationField;
import com.hx.vending.annotation.Authorization;
import com.hx.vending.annotation.CurrentFieldId;
import com.hx.vending.annotation.CurrentUserId;
import com.hx.vending.bean.GoodsConsume;
import com.hx.vending.bean.OnlineOrder;
import com.hx.vending.bean.entity.GoodsDetailsVO;
import com.hx.vending.bean.entity.GoodsOrderVO;
import com.hx.vending.service.*;
import com.hx.vending.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/12/7.
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOnlineOrderService onlineOrderService;
    @Autowired
    private IUnderlineOrderService underlineOrderService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IRefundService refundService;
    @Autowired
    private IWXPayService wxPayService;
    /**
     * 代理商下当天线上消费记录头信息
     * @param   userId      商户ID
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/selectByUserIdToday", method = RequestMethod.POST)
    @Authorization
    @AnnotationField
    public Msg selectByUseridToday(@CurrentUserId Integer userId, String startTime, String endTime,@CurrentFieldId Integer[] integers) {
        return onlineOrderService.selectOnlineOrderUserId(userId,startTime,endTime,integers);
    }

    /**
     * 根据商家id查询线上消费记录 startTime,endTime/orderDealNumber/orderState
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderVoList", method = RequestMethod.POST)
    @Authorization
    @AnnotationField
    public Msg getOrderVoList(@RequestBody Map<String,Object> paramMap,@CurrentUserId Integer userId,@CurrentFieldId Integer[] integers) {
        return onlineOrderService.getOnlineOrderList(paramMap,userId,integers);
    }

    /**
     *订单查询
     * @param paramMap
     * @param userId
     * @param integers
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOrderList", method = RequestMethod.POST)
    @Authorization
    @AnnotationField
    public Msg getOrderList(@RequestBody Map<String,Object> paramMap,@CurrentUserId Integer userId,@CurrentFieldId Integer[] integers){
        return onlineOrderService.getOrderList(paramMap,userId,integers);
    }

    /*
    *线下收益列表
    * */
    @ResponseBody
    @RequestMapping(value = "/getUnderlineOrderList",method = RequestMethod.POST)
    @Authorization
    public Msg getUnderlineOrderList(@RequestBody Map<String,Object> paramMap,@CurrentUserId Integer userId){
        return underlineOrderService.getUnderlineOrderList(paramMap,userId);
    }
     /*
    *线下收益头信息
    * */
    @ResponseBody
    @RequestMapping("/getUnderlineOrder")
    @Authorization
    public Msg getUnderlineOrder(@CurrentUserId Integer userId,String startTime,String endTime){
        return underlineOrderService.getUnderlineOrder(userId,startTime,endTime);
    }

    /*
    * 商品消耗头信息
    * */
    @ResponseBody
    @RequestMapping("/getGoodsConsume")
    @Authorization
    @AnnotationField
    public Msg getGoodsConsume(@CurrentUserId Integer userId,String startTime,String endTime,@CurrentFieldId Integer[] integers){
        return goodsService.getGoodsConsume(userId,startTime,endTime,integers);
    }
    /*
    * 用户下商品消耗记录
    * */
    @ResponseBody
    @RequestMapping("/getGoodsConsumeList")
    @Authorization
    @AnnotationField
    public Msg getGoodsConsumeList(@CurrentUserId Integer userId,@CurrentFieldId Integer[] integers,@RequestParam String startTime,@RequestParam String endTime,
                                       @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum){
        return goodsService.getGoodsConsumeList(userId,integers,startTime,endTime,pageNum);
    }

    /**
     * 商品消耗详情
     * @return
     */
    @RequestMapping("/getGoodsConsumeDetails")
    @ResponseBody
    @Authorization
    public Msg getGoodsConsumeDetails(Integer onlineOrderId){
        return goodsService.getGoodsConsumeDetails(onlineOrderId);
    }


    /**
     * 线上榜单排行
     * @param startTime       开始时间
     * @param endTime         结束时间
     * @param pageNum         页码
     * @param rankingNumber   查询数量(可传可不传)
     * @return
     */
    @PostMapping("/selectListRanking")
    @ResponseBody
    public Msg selectListRanking(String startTime, String endTime, @RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum, @RequestParam(value = "rankingNumber", defaultValue = "10")Integer rankingNumber) {

        return this.onlineOrderService.selectListRanking(startTime, endTime, pageNum, rankingNumber);
    }


    /**
     * 根据线上订单ID查询信息
     * @param onlineOrderId
     * @return
     */
    @GetMapping("/selectOnlineOrderById")
    @ResponseBody
    public Msg selectOnlineOrderById(Integer onlineOrderId) {

        return this.onlineOrderService.selectOnlineOrderById(onlineOrderId);
    }

    /**
     * 根据订单号查询线上订单
     * @param merchantNumber
     * @return
     */
    @RequestMapping(value = "/getOnlineOrderByMerchantNumber",method = RequestMethod.GET)
    @ResponseBody
    @Authorization
    public Msg getOnlineOrderByMerchantNumber(String merchantNumber){
        return onlineOrderService.getOnlineOrderByMerchantNumber(merchantNumber);
    }

    /**
     * 提现
     * @param onlineOrderId 订单id
     * @param orderBuyId 详情id
     * @param money 金额
     * @return
     */
    @RequestMapping("/withdraw")
    @ResponseBody
    @Authorization
    public Msg withdraw(Integer onlineOrderId,Integer orderBuyId,Double money){
        System.out.println("============订单id"+onlineOrderId);
        System.out.println("============商品详情id"+orderBuyId);
        System.out.println("============退款金额"+money);
        OnlineOrder o = new OnlineOrder();
        o.setOnlineOrderId(onlineOrderId);
        OnlineOrder onlineOrder = onlineOrderService.selectByPrimaryKeySelective(o);
        if(onlineOrder.getOnlineOrderType().equals("1")){
            return wxPayService.wxRefund(onlineOrderId, money, orderBuyId);
        }else if(onlineOrder.getOnlineOrderType().equals("2")){
            return refundService.aliPayRefund(onlineOrderId,orderBuyId,money);
        }
        return Msg.fail();
    }

    /**
     * 根据设备查询订单信息
     * @param equipmentId
     * @return
     */
    @RequestMapping("/selectOrder")
    @ResponseBody
        public Msg selectOrderByEquipment(String equipmentId,@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize){
        pageNum = (pageNum - 1) * 10;
        return onlineOrderService.selectOrderByEquipment(equipmentId,pageNum,pageSize);
    }

}
